package com.syni.mdd.yhd.common.all.controller.protect.business;

/**
 * @className BmsBusinessCouponController
 * @description TOO
 * @Author hwq
 * @DATE 2019/9/23 10:31
 * @VERSION 1.0
 **/

import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.sjb.common.component.utils.common.annotations.EntranceCheck;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dto.requestDto.business.BusinessCouponReDto;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessCoupon;
import com.syni.mdd.yhd.common.all.entity.BmsDxOrder;
import com.syni.mdd.yhd.common.all.service.BmsBusinessCouponService;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderService;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.redis.utils.RedisTemplateUtils;;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.validationGroup.GroupCheckPaging;
import com.syni.mdd.yhd.common.all.validationGroup.GroupFind;
import com.syni.mdd.yhd.common.all.validationGroup.GroupInsert;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
@Log4j2
@RestController
@RequestMapping("/businessCoupon")
public class BmsBusinessCouponController {
    @Autowired
    BmsBusinessCouponService bmsBusinessCouponService;

    @Autowired
    BmsDxOrderService bmsDxOrderService;

    /**
     * @Description 生成新劵
     * @Param GroupChatReDto
     * @return Map
     * @Author: hwq
     * @Date: 2019/9/23
     */
    @PostMapping("/create")
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> createCoupon(@RequestBody @Validated({GroupInsert.class}) BusinessCouponReDto businessCouponReDto){
        return bmsBusinessCouponService.createCoupon(businessCouponReDto);
    }

    /**
     * @Description 商家劵列表
     * @Param showCouponList
     * @return Map
     * @Author: hwq
     * @Date: 2019/9/23
     */
    @PostMapping("/showCouponList")
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> showCouponList(@RequestBody BusinessCouponReDto businessCouponReDto){
        return bmsBusinessCouponService.showCouponList(businessCouponReDto);
    }

    /**
     * @Description 劵详情
     * @Param showCouponDetail
     * @return Map
     * @Author: hwq
     * @Date: 2019/9/23
     */
    @PostMapping("/showCouponDetail")
    @EntranceCheck
    public Map<String,Object> showCouponDetail(@RequestBody BusinessCouponReDto businessCouponReDto){
        return bmsBusinessCouponService.showCouponDetail(businessCouponReDto);
    }


    /**
     * @Description 下线商家劵
     * @Param offCoupon
     * @return Map
     * @Author: hwq
     * @Date: 2019/9/23
     */
    @PostMapping("/offCoupon")
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> offCoupon(@RequestBody BusinessCouponReDto businessCouponReDto){
        return bmsBusinessCouponService.offCoupon(businessCouponReDto);
    }

    /**
     * @Description 用户端获取商家劵列表
     * @return Map
     * @Author: hwq
     * @Date: 2019/9/23
     */
    @PostMapping("/userGetBusinessCoupon")
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> userGetBusinessCoupon(@RequestBody @Validated(value = {GroupFind.class, GroupCheckPaging.class}) BusinessCouponReDto businessCouponReDto){
        return bmsBusinessCouponService.userGetBusinessCoupon(businessCouponReDto);
    }

    /**
     * @Description 用户端领券
     * @Param showCouponList
     * @return Map
     * @Author: hwq
     * @Date: 2019/9/23
     */
    @PostMapping("/userReceiveBusinessCoupon")
    @CheckParams(checkAuth = true)
    public Map<String,Object> userReceiveBusinessCoupon(@RequestBody ParamsMap paramsMap){
        Map<String,Object> resultMap;
        Integer businessCouponId = paramsMap.getInteger("businessCouponId");
        //使用分布式锁
        if(!(RedisTemplateUtils.addLock(Constants.REDIS_KEY_BUSINESS_COUPON_REC + businessCouponId))) {
            return ResultMapHelper.result(ResultCode.CODE_ERROR_100,ResultMsg.HAND_FAIL);
        }
        try {
            resultMap = bmsBusinessCouponService.userReceiveBusinessCoupon(paramsMap);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            resultMap = ResultMapHelper.result(ResultCode.CODE_ERROR_100, ResultMsg.HAND_FAIL);
        } finally {
            //分布式锁释放
            RedisTemplateUtils.releaseLock(Constants.REDIS_KEY_BUSINESS_COUPON_REC + businessCouponId);
        }
        return resultMap;
    }


    @GetMapping("/test")
    public String createCoupon(){
        BmsBusinessCoupon bmsBusinessCoupon = new BmsBusinessCoupon();
        Date time = DateTimeUtils.converStringToDate("2019.10.11 20:00",DateTimeUtils.DATE_PATTERN_TIMESTAMP_04);
        bmsBusinessCoupon.setSendStartTime(time);
        bmsBusinessCouponService.getBaseDao().save(bmsBusinessCoupon);

        BmsDxOrder bmsDxOrder = new BmsDxOrder();
        bmsDxOrder.setTimingCancelTime(time);
        bmsDxOrderService.getBaseDao().save(bmsDxOrder);
        return "s";

    }

}
